package helloworld.admin.action.teacher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import helloworld.admin.bean.TeacherAdminBean;
import helloworld.admin.form.teacher.*;
import helloworld.delegate.teacher.TeacherDelegate;
import helloworld.delegate.teacher.TeacherListDelegate;

/** 
 * @author TUYEN-DM
 *         <p>
 *         2009/05/13
 *         </p>
 *         <p>
 *         ManageTeachersAction is the object to process actions for ManageTeachersForm.
 *         </p>
 */
public class ManageTeachersAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionForward forward = null;
		ManageTeachersForm teacherForm = (ManageTeachersForm) form;

		String strAction = request.getParameter("submitAction");

		if (strAction.equals("search")) {
			TeacherListDelegate delegate = new TeacherListDelegate();
			TeacherAdminBean[] teachers = delegate.findTeachers(teacherForm.getTeacherName());
			request.setAttribute("CUSTOMER_SUMMARY_OBJECTS", teachers);
			forward = mapping.findForward("success");
		}

		if (strAction.equals("delete")) {
			TeacherDelegate delegate = new TeacherDelegate();
			String[] idsToDelete = teacherForm.getIdSelections();
			if (idsToDelete != null && idsToDelete.length > 0) {
				for (int i = 0; i < idsToDelete.length; i++) {
					int x = Integer.parseInt(idsToDelete[i]);
					delegate.deleteTeacher(x);
				}
			}
			forward = mapping.findForward("deleteSuccess");
		}

		if (strAction.equals("new")) {
			forward = mapping.findForward("add");
		}

		return forward;
	}
}
